﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Collections;
using System.Text;
using System.Data;

namespace Catering.Catering_Page.Caipin
{
    public partial class select_Date : UserIsNull
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["IsGroupCatering"] != null)
            {
                this.HidIsQun.Value = "true";
                Catering_Model.GroupCatering md_qunti = new Catering_Rules.GroupCatering().GetModel(Convert.ToInt32(Request.QueryString["cid"]));
                this.HidClassID.Value = md_qunti.ClaassID.ToString();
            }
        }

        protected void Next_But_Click(object sender, EventArgs e)
        {
            string cid=Request.QueryString["cid"];//配餐ID（群体或个人）
            string strdatetime = this.hf_datelist.Value;//选择的时间段
            string[] datetimes = strdatetime.TrimEnd(',').Split(',');//时间周期

            DateTime starttime = Convert.ToDateTime(datetimes[0].Split('|')[0]);
            DateTime endtime = Convert.ToDateTime(datetimes[datetimes.Length-1].Split('|')[1]);

            if (Request.QueryString["IsGroupCatering"] != null)//群体配餐
            {
                Catering_Model.GroupCatering md_qunti=new Catering_Rules.GroupCatering().GetModel(Convert.ToInt32(cid));
                Catering_Model.E_Recipes md = new Catering_Model.E_Recipes();
                md.CateringName = md_qunti.GCName;
                md.ClassID = md_qunti.ClaassID;
                md.GroupID = md_qunti.id;
                md.PID = 0;//表明为群体配餐
                md.StateDate = starttime;
                md.StopDate = endtime;
                md.Number = new Catering_Rules.CateringGroupInfo().GetSumRSByGroupID(cid);
                md.AreaID = md_qunti.AreaID;
                md.ClassIDs = md_qunti.ClaassID;
                md.EditPersonnelID = LoginPersonnel.id;
                md.EditTime = DateTime.Now;
                new Catering_Rules.T_Recipes().Add(md);
                this.SaveRecipesWeek(datetimes, md.id);
                Response.Redirect("Caipin_jcsz/shipu_clone05.aspx?cid=" + md.id + "&IsGroupCatering=true");
            }
            else //个人配餐
            {
                Catering_Model.PersonalService md_geren = new Catering_Rules.PersonalService().GetModel(Convert.ToInt32(cid));
                Catering_Model.E_Recipes md = new Catering_Model.E_Recipes();
                md.CateringName = md_geren.CateringName;
                md.PID = md_geren.id;
                md.StateDate = starttime;
                md.StopDate = endtime;
                md.Number = 1;
                md.EditPersonnelID = LoginPersonnel.id;
                md.EditTime = DateTime.Now;
                new Catering_Rules.T_Recipes().Add(md);
                this.SaveRecipesWeek(datetimes, md.id);
                Response.Redirect("shipu_left.aspx?cid=" + md.id);
            }
        }

        //保存辅助周期
        public void SaveRecipesWeek(string[] datetimes, int RecipesID)
        {
            for (int i = 0; i < datetimes.Length; i++)
            {
                Catering_Model.E_RecipesWeek md_week = new Catering_Model.E_RecipesWeek();
                md_week.startDate = Convert.ToDateTime(datetimes[i].Split('|')[0]);
                md_week.endDate = Convert.ToDateTime(datetimes[i].Split('|')[1]);
                md_week.Number = i;
                md_week.RecipesID = RecipesID;
                md_week.temp1 = "第" + (i + 1).ToString() + "周";
                md_week.id = new Catering_Rules.T_RecipesWeek().GetMaxId();
                new Catering_Rules.T_RecipesWeek().Add(md_week);
            }
        }

        //验证日期
        [WebMethod]
        public static string GetDatatime(string starttime,string endtime,string ClassID)
        {
            StringBuilder sb = new StringBuilder();
            DateTime Start = Convert.ToDateTime(starttime);
            DateTime End = Convert.ToDateTime(endtime);
            DataTable dt_Week = new Catering_Rules.T_RecipesWeek().GetListByClassID(Convert.ToInt32(ClassID), Start, End).Tables[0];
            if (dt_Week != null && dt_Week.Rows.Count > 0)
            {
                foreach (DataRow row in dt_Week.Rows)
                {
                    string Stime = Convert.ToDateTime(row["startDate"].ToString()).ToString("yyyy-MM-dd");
                    string Etime = Convert.ToDateTime(row["endDate"].ToString()).ToString("yyyy-MM-dd");
                    sb.Append(Stime + "|" + Etime + ",");
                }
            }
            return sb.ToString().TrimEnd(',');
        }
    }
}